/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Modelo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author Ing Arturo
 */
@Entity
public class Factor implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String nombre;
    private String descripcion;
    @OneToMany// (cascade = CascadeType.MERGE)
    private List<Caracteristica> caracteristica;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Factor() {
    caracteristica=new ArrayList<Caracteristica>();
    }


    public List<Caracteristica> getCaracteristica() {
        return caracteristica;
    }

    public void setCaracteristica(List<Caracteristica> caracteristica) {
        this.caracteristica = caracteristica;
    }

    public String getDescripcion() {
        return descripcion;
    }

    public void setDescripcion(String descripcion) {
        this.descripcion = descripcion;
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Factor)) {
            return false;
        }
        Factor other = (Factor) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Modelo.Factor[id=" + id + "]";
    }

      public Long agregarFactor(Factor factor) {



        EntityManagerFactory emf = Persistence.createEntityManagerFactory(
                "APU");

        // Crear el EntityManager para accesso a la capa de persistencia
        EntityManager em = emf.createEntityManager();
        // Se es transacciÃƒÆ’Ã†â€™Ãƒâ€ Ã¢â‚¬â„¢ÃƒÆ’Ã¢â‚¬Å¡Ãƒâ€šÃ‚Â³n de escritura, obtener la transacciÃƒÆ’Ã†â€™Ãƒâ€ Ã¢â‚¬â„¢ÃƒÆ’Ã¢â‚¬Å¡Ãƒâ€šÃ‚Â³n
        // del EntityManager e iniciarla
        em.getTransaction().begin();

        // Marcar el registro en la base de datos
        em.persist(factor);

        // Ejecutar la transacciÃƒÆ’Ã†â€™Ãƒâ€ Ã¢â‚¬â„¢ÃƒÆ’Ã¢â‚¬Å¡Ãƒâ€šÃ‚Â³n
        em.getTransaction().commit();
         Query q = em.createQuery("select Max(f.id) from Factor f");
        return (Long) q.getSingleResult();

    }


    public void actualizarFactor(Factor factor) {



        EntityManagerFactory emf = Persistence.createEntityManagerFactory(
                "APU");

        // Crear el EntityManager para accesso a la capa de persistencia
        EntityManager em = emf.createEntityManager();
        // Se es transacciÃƒÆ’Ã†â€™Ãƒâ€ Ã¢â‚¬â„¢ÃƒÆ’Ã¢â‚¬Å¡Ãƒâ€šÃ‚Â³n de escritura, obtener la transacciÃƒÆ’Ã†â€™Ãƒâ€ Ã¢â‚¬â„¢ÃƒÆ’Ã¢â‚¬Å¡Ãƒâ€šÃ‚Â³n
        // del EntityManager e iniciarla
        em.getTransaction().begin();

        // Marcar el registro en la base de datos
        em.merge(factor);

        // Ejecutar la transacciÃƒÆ’Ã†â€™Ãƒâ€ Ã¢â‚¬â„¢ÃƒÆ’Ã¢â‚¬Å¡Ãƒâ€šÃ‚Â³n
        em.getTransaction().commit();

    }


     public Factor findFactor(Long cedula) {


        Factor factor = new Factor();

        EntityManagerFactory emf = Persistence.createEntityManagerFactory(
                "APU");

        // Crear el EntityManager para accesso a la capa de persistencia
        EntityManager em = emf.createEntityManager();
        // Se es transacciÃƒÆ’Ã†â€™Ãƒâ€ Ã¢â‚¬â„¢ÃƒÆ’Ã¢â‚¬Å¡Ãƒâ€šÃ‚Â³n de escritura, obtener la transacciÃƒÆ’Ã†â€™Ãƒâ€ Ã¢â‚¬â„¢ÃƒÆ’Ã¢â‚¬Å¡Ãƒâ€šÃ‚Â³n
        // del EntityManager e iniciarla
        em.getTransaction().begin();

        // Marcar el registro en la base de datos
        factor = em.find(Factor.class, cedula);

        // Ejecutar la transacciÃƒÆ’Ã†â€™Ãƒâ€ Ã¢â‚¬â„¢ÃƒÆ’Ã¢â‚¬Å¡Ãƒâ€šÃ‚Â³n
        em.getTransaction().commit();

        return factor;

    }

      public List getLista() {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("APU");
        EntityManager em = emf.createEntityManager();
        Query q = em.createQuery("SELECT f FROM Factor f");
        return q.getResultList();
    }
       public void asignarC(Long idf, Caracteristica c) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("APU");
        EntityManager em = emf.createEntityManager();
        Factor f = em.find(Factor.class, idf);
        f.addC(c);
        em.getTransaction().begin();
        em.persist(f);
        em.getTransaction().commit();
        em.close();
        emf.close();
    }

    private void addC(Caracteristica c) {
        this.caracteristica.add(c);
    }
}
